﻿-- Author      : Kurapica

--------------------------
-- WidgetSeparator        --
--------------------------
do
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = IGAS.GUI.CreateFrame
    
    local _WidgetName = "WidgetSeparator"
    local _Base = IGAS.GUI.Widget.Frame

    local _Widget = IGAS.Studio.Widget
    local _PropertyType = IGAS.GUI.Base.PropertyType

    -- Script	
	local function OnMouseDown(self)
		self.Parent.ToggleState = not self.Parent.ToggleState
	end

    -- ScriptType
    local _ScriptType = {
    }

    --    FuncProxy
    local _FuncProxy = {
		-- Dispose, release resource
        ["Dispose"] = function(self)
			local index = self.ID
			local parent = self.Parent
				
			self.ToggleState = false
			
			-- Dispose Childs
			if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
				for i = self.__WidgetButtonNum, 1, -1 do
					if self:GetChild("WidgetButton"..i) then
						self:GetChild("WidgetButton"..i):Dispose()
					end
				end
			end
			
			self:ClearAllPoints()
			
			if parent:GetChild(_WidgetName..(index+1)) then
				if index == 1 then
					parent:GetChild(_WidgetName..(index+1)):SetPoint("TOP", parent, "TOP", -10, 0)
				else
					parent:GetChild(_WidgetName..(index+1)):SetPoint("TOP", parent:GetChild(_WidgetName..(index - 1)), "BOTTOM", 0, 0)
				end
			end
			
			self:Hide()
			
			parent:RemoveChild(self)
			
			if parent.__WidgetSeparatorNum > index then
				for i = index + 1, parent.__WidgetSeparatorNum do
					parent:GetChild(_WidgetName..i).Name = _WidgetName..(i - 1)
				end
			end
			
			parent.__WidgetSeparatorNum = parent.__WidgetSeparatorNum - 1
			
			parent.Height = parent.Height - 26
			
            -- Call super's dispose
			self:CallMethod(_Base, "Dispose")
        end,
		
		-- GetToggleState
		["GetToggleState"] = function(self)
			return (self.__ToggleState and true) or false
		end,
		
		-- SetToggleState
		["SetToggleState"] = function(self, flag)
			flag = (flag and true) or false
			
			if self.__ToggleState == flag then
				return
			end
			
			self.__ToggleState = flag
			
			if flag then
				if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
					for i = 1, self.__WidgetButtonNum do
						self:GetChild("WidgetButton"..i).Visible = true
						if i == 1 then
							self:GetChild("WidgetButton"..i):SetPoint("TOP", self.TextBtn, "BOTTOM", 0, -6) 
						else
							self:GetChild("WidgetButton"..i):SetPoint("TOP", self:GetChild("WidgetButton"..(i-1)), "BOTTOM", 0, -6)
						end
					end
					
					self.Height = 26 + self.__WidgetButtonNum * 26
					
					self.Parent.Height = self.Parent.Height + self.__WidgetButtonNum * 26
				end
			else
				if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
					for i = 1, self.__WidgetButtonNum do
						self:GetChild("WidgetButton"..i).Visible = false
					end
					
					self.Parent.Height = self.Parent.Height - self.__WidgetButtonNum * 26
				end
				
				self.Height = 26
			end
		end,
		
		-- AddWidget
		["AddWidget"] = function(self, widget)
			if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
				for i = 1, self.__WidgetButtonNum do
					if self:GetChild("WidgetButton"..i).Widget == widget then
						return self:GetChild("WidgetButton"..i)
					end
				end
			end
			
			local btn = CreateFrame(_Widget["WidgetButton"], nil, self)
			btn.Widget = widget
			
			if self.ToggleState then
				self.Height = self.Height + 26
				self.Parent.Height = self.Parent.Height + 26
			end
			
			if btn.ID == 1 then
				btn:SetPoint("TOP", self.TextBtn, "BOTTOM", 0, -6)
			else
				btn:SetPoint("TOP", self:GetChild("WidgetButton"..(btn.ID-1)), "BOTTOM", 0, -6)
			end
			
			return btn
		end,
		
		-- RemoveWidget
		["RemoveWidget"] = function(self, widget)
			if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
				for i = 1, self.__WidgetButtonNum do
					if self:GetChild("WidgetButton"..i).Widget == widget then
						if i == 1 then
							if self.__WidgetButtonNum > 1 then
								self:GetChild("WidgetButton2"):SetPoint("TOP", self.TextBtn, "BOTTOM", 0, -6)
							end
						elseif self.__WidgetButtonNum > i then
							self:GetChild("WidgetButton"..(i+1)):SetPoint("TOP", self:GetChild("WidgetButton"..(i-1)), "BOTTOM", 0, -6)
						end
						
						self:GetChild("WidgetButton"..i):Dispose()
						
						if i < self.__WidgetButtonNum then
							for j = i+1, self.__WidgetButtonNum do
								self:GetChild("WidgetButton"..j).Name = "WidgetButton"..(j-1)
							end
						end
						
						if self.ToggleState then
							self.Height = self.Height - 26
							self.Parent.Height = self.Parent.Height - 26
						end
						
						return
					end
				end
			end
		end,
    }

    --    Property
    local _Property = {		
		-- ToggleState
		["ToggleState"] = {
			["Set"] = function(self, flag)
				self:SetToggleState(flag)
			end,

			["Get"] = function(self)
				return self:GetToggleState()
			end,

			["Type"] = _PropertyType.Boolean,
		},
		-- Text
		["Text"] = {
			["Set"] = function(self, text)
				self.TextBtn.Text = text
			end,
			
			["Get"] = function(self)
				return self.TextBtn.Text
			end,
			
			["Type"] = _PropertyType.String,
		},
    }

    local function _New(parent)-- Base Frame
        local frame = CreateFrame("Frame", nil, parent)
		
		frame.Height = 26
		frame.Width = 180
		frame.__ToggleState = true
		
        local btn = CreateFrame("Button", "TextBtn", frame)

        btn.Style = "GRAY"
        btn.Height = 20
        btn.Width = 160
        btn:SetPoint("TOP", frame, "TOP", 0, 0)
        btn.Locked = true

        -- Parent's Settings
        local index = parent.__WidgetSeparatorNum or 0
		index = index + 1
		
		if index == 1 then
			frame:SetPoint("TOP", parent, "TOP", -10, 0)
		else
			frame:SetPoint("TOP", parent:GetChild(_WidgetName..(index - 1)), "BOTTOM", 0, 0)
		end
        
		parent.__WidgetSeparatorNum = index
		parent.Height = parent.Height + 26
		frame.ID = index
		
        btn:StackScript("OnMouseDown", OnMouseDown, true)
		
		return frame
    end

    -- Register
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }

    _Widget[_WidgetName] = IGAS:NewWidget(_WidgetInfo)
end
